package com.breakfast.fun.adapter.mall;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseListAdapter;
import com.breakfast.fun.data.entity.mall.MallOrder;
import com.breakfast.fun.event.PayEvent;
import com.breakfast.fun.ui.home.comment.CommentActivity;
import com.breakfast.fun.ui.user.order.MallOrderDetailActivity;
import com.breakfast.fun.util.JumpUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/30.
 */
public class MallOrderAdapter extends BaseListAdapter<MallOrder.DataBean> {
    public MallOrderAdapter(Context context, List<MallOrder.DataBean> list) {
        super(context, list);
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_user_bforder, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MallOrder.DataBean data = getItem(i);

        holder.tvItemBforderTime.setText(data.getOrderTime().substring(0, data.getOrderTime().length() - 3));
        holder.tvItemBforderCount.setText("" + data.getQuantity());
        holder.tvItemBforderPrice.setText("￥" + String.format("%.2f", data.getTotalPrice()) + "元");

        if (data.getState() == 0) {
            //待支付
            holder.tvItemBforderState.setText("待付款");
            holder.tvItemBforderAction.setText("去付款");
            holder.tvItemBforderAction.setTextColor(Color.parseColor("#D90504"));
            holder.tvItemBforderAction.setBackgroundResource(R.drawable.rectangle_red);
            holder.tvItemBforderAction.setOnClickListener(v -> {
                EventBus.getDefault().post(new PayEvent(data.getOrderNo(), "MallOrderFragment"));
            });

        } else if (data.getState() == 1) {
            //已支付
            holder.tvItemBforderState.setText("未评价");
            holder.tvItemBforderAction.setText("去评价");
            holder.tvItemBforderAction.setTextColor(Color.parseColor("#F19601"));
            holder.tvItemBforderAction.setBackgroundResource(R.drawable.rectangle_orange);
            holder.tvItemBforderAction.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, CommentActivity.class);
                intent.putExtra("orderId", data.getId() + "");
                intent.putExtra("orderNo", data.getOrderNo());
                intent.putExtra("orderTime", data.getOrderTime());
                intent.putExtra("orderType", 1);
                mContext.startActivity(intent);
            });
        } else if (data.getState() == 2) {
            //已评价
            holder.tvItemBforderState.setText("交易成功");
            holder.tvItemBforderAction.setText("再来一单");
            holder.tvItemBforderAction.setTextColor(Color.parseColor("#F19601"));
            holder.tvItemBforderAction.setBackgroundResource(R.drawable.rectangle_orange);
            holder.tvItemBforderAction.setOnClickListener(v -> {
                JumpUtil.toHome(mContext);
            });
        }


        List<MallOrder.DataBean.GoodsBean> beanList = data.getGoods();

        View view = null;

        for (MallOrder.DataBean.GoodsBean bean : beanList) {
            view = mInflater.inflate(R.layout.item_bforder, null);
            TextView tvItemBforderName = (TextView) view.findViewById(R.id.tv_item_bforder_name);
            TextView tvItemBforderSendDate = (TextView) view.findViewById(R.id.tv_item_bforder_send_date);
            ImageView ivItemBforder = (ImageView) view.findViewById(R.id.iv_item_bforder);

            tvItemBforderName.setText(bean.getProductName());
            tvItemBforderSendDate.setVisibility(View.GONE);
            imageUtil.loadUrlImage(bean.getProductImage(), ivItemBforder);
            holder.llItemBforderList.addView(view);
        }


        holder.llItemBforderList.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, MallOrderDetailActivity.class);
            intent.putExtra("orderId", data.getId() + "");
            intent.putExtra("orderNo", data.getOrderNo());
            mContext.startActivity(intent);
        });

        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.tv_item_bforder_tip)
        TextView tvItemBforderTip;
        @Bind(R.id.tv_item_bforder_time)
        TextView tvItemBforderTime;
        @Bind(R.id.tv_item_bforder_state)
        TextView tvItemBforderState;
        @Bind(R.id.ll_item_bforder_list)
        LinearLayout llItemBforderList;
        @Bind(R.id.tv_item_bforder_count)
        TextView tvItemBforderCount;
        @Bind(R.id.tv_item_bforder_price)
        TextView tvItemBforderPrice;
        @Bind(R.id.tv_item_bforder_action)
        TextView tvItemBforderAction;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
